
var DBGrid = {

    store : null,

    overColor : '#F6F6F4',

    outColor : '#ffffff',

    /*
     * page link
     */
    clickPageLink: function(page){
        $("#dbgridAction").val("");
        $("#currentPageNumber").val(page);
        $("#dbgridForm").submit();
    },

    clickAjaxPageLink: function(page){
        $("#dbgridAction").val("");
        DBGrid.searchAjaxDbGrid(page);
    },

    search:  function(){
        $("#dbgridAction").val("");
        this.clearSort();
        $("#currentPageNumber").val(1);
        $("#advanceSearchStatus").val(0);
        $("#dbgridForm").submit();
    },

    ajaxSearch:  function(url){
        var data = $("#dbgridForm").serialize();
        $.ajax({
            type:"POST",
            url:url,
            data: data,
            dataType:"Html",
            success:function(responseText){
                $("#dbgrid_centerbig").empty();
                $("#dbgrid_centerbig").html(responseText);
            }
        });
    },

    searchAjaxDbGrid: function(currentPageNumber){
        var url = $("#dbgridForm").attr("action");
        $("#currentPageNumber").val(currentPageNumber);
        $("#useAjax").val(1);
        var data = $("#dbgridForm").serialize();
        $.ajax({
            type:"POST",
            url:url,
            data: data,
            dataType:"Html",
            success:function(responseText){
                $("#dbgrid_centerbig").empty();
                $("#dbgrid_centerbig").html(responseText);
            }
        });
    },

    advanceSearch:  function(){
        $("#dbgridAction").val("");
        this.clearSort();
        $("#currentPageNumber").val(1);
        $("#advanceSearchStatus").val(1);
        $("#dbgridForm").submit();
    },

    editBtn: function(url){
            var item_checked;
            var items = $("input[name='dbgrid_item_ckb']");
            $.each(items,function(i,obj){
                if($(obj).attr("checked") == true){
                    if(!item_checked){
                        item_checked = $(obj).val();
                        url = url + "/id/" + item_checked;
                        DBGrid.gotoUrl(url);
                    }
                }
            });
            if(!item_checked){
               alert("Please select a record.");
            }
    },

    deleteBtn: function(url){
            var item_checked = Array();
            var items = $("input[name='dbgrid_item_ckb']");
            $.each(items,function(i,obj){
                if($(obj).attr("checked") == true){
                    item_checked.push($(obj).val());
                }
            });
            if(!item_checked){
               alert("Please select a record.");
            }else{
               if(confirm('Are you sure you want to do this?')){
                   $("#selectIds").val(item_checked.join(","));
                   $("#dbgridForm").attr("action",url);
                   $("#dbgridForm").submit();
               }
            }
    },

    gotoUrl: function(url){
        window.location = url;
    },

    checkAll: function(){
        var status = $("#dbgrid_all_ckb").attr("checked");
        var items = $("input[name='dbgrid_item_ckb']");
        $.each(items,function(i,obj){
           $(obj).attr("checked",status);
        });
        
    },

    changeRowStyle: function(obj,i){
        if(i == 1){
            obj.style.background = this.overColor;
        }else{
            obj.style.background = this.outColor;
        }

    },

    saveBtn: function(){
        $("form").submit();
    },

    clickCol: function(col,order){
        $("#sortCol").val(col);
        $("#sortOrder").val(order);
        $("#currentPageNumber").val(1);
        $("#dbgridAction").val("");
        $("#dbgridForm").submit();
    },

    clickAjaxCol: function(col,order){
        $("#sortCol").val(col);
        $("#sortOrder").val(order);
        $("#currentPageNumber").val(1);
        $("#dbgridAction").val("");
        DBGrid.searchAjaxDbGrid(1);
    },

    changePageOption: function(obj){
        $("#dbgridAction").val("");
        this.clearSort();
        $("#currentPageNumber").val(1);
        $("#dbgridPageOption").val(obj.value);
        $("#dbgridForm").submit();
    },

    changeAjaxPageOption: function(obj){
        $("#dbgridAction").val("");
        this.clearSort();
        $("#currentPageNumber").val(1);
        $("#dbgridPageOption").val(obj.value);
        DBGrid.searchAjaxDbGrid(1);
    },

    clearSort: function(){
        $("#sortCol").val("");
        $("#sortOrder").val("");
    },

    exportData: function(){
        $("#dbgridAction").val("export");
        $("#dbgridForm").submit();
    },

    showOrHideAdvanceForm: function(){
          $("#dbgrid_advance_filter").slideToggle("slow");
    },

    setAdvanceSearchStatus: function(){
        if($("#advanceSearchStatus").val() == "1"){
            //$("#dbgrid_advance_filter").html($("#dbgrid_advance_filter_hide").html());
            $("#dbgrid_advance_filter").slideDown("slow");
        }
    },

    returnData: function(method){
        var data = new Array();
        var items = $("input[name='dbgrid_item_ckb']");
        $.each(items,function(i,obj){
            if($(obj).attr("checked") == true){
                var itemCheckedId = $(obj).val();
                data.push(DBGrid.store[itemCheckedId]);
            }
        });
        method(data);
    }
}